/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年6月14日
 */
package com.massyframework.beanskin.runtime.service.serialize;

import com.massyframework.beanskin.ModuleRepository;
import com.massyframework.beanskin.framework.FrameworkConfig;
import com.massyframework.beanskin.launching.LaunchingHandler;
import com.massyframework.beanskin.service.metadata.MetadataRepository;
import com.massyframework.beanskin.service.serialize.MetadataSerializer;
import com.massyframework.beanskin.service.serialize.ModuleSerializer;
import com.massyframework.beanskin.service.serialize.PlatformSerializer;

/**
 * 序列化器服务初始化
 * @author huangkh
 *
 */
public final class SerializerInitializer implements LaunchingHandler {

	/**
	 * 
	 */
	public SerializerInitializer() {
	}

	@Override
	public void init(FrameworkConfig config) {
		MetadataRepository metadataRepo = 
				config.getBean(MetadataRepository.class.getName(), MetadataRepository.class);
		ModuleRepository moduleRepository =
				config.getBean(ModuleRepository.class.getName(), ModuleRepository.class);
		
		//注册平台序列化器
		DefaultPlatformSerializer platformSerilizer = new DefaultPlatformSerializer(moduleRepository);
		config.defineExportService(PlatformSerializer.class.getName(), platformSerilizer, builder-> {
			builder.name("jdk")
				.addExportService(PlatformSerializer.class)
				.description("支持在多个massyframework进程间传输可序列化对象")
				.topRanking();
		});
				
		//注册元数据对象序列化器
		DefaultMetadataSerailzier metadataSerializer = new DefaultMetadataSerailzier(metadataRepo);
		config.defineExportService(MetadataSerializer.class.getName(), metadataSerializer, builder-> {
			builder.name("jdk")
				.addExportService(MetadataSerializer.class)
				.description("支持在massyframework进程和非massyframework进程间传输可序列化对象")
				.topRanking();
		});
		
		//注册模块序列化器
		ModuleSerializerServiceFactory factory = new ModuleSerializerServiceFactory();
		config.defineExportService(ModuleSerializer.class.getName(), factory, builder-> {
			builder.name("jdk")
				.isFactory(true)
				.addExportService(ModuleSerializer.class)
				.description("支持在massyframework进程相同模块之间传输可序列化对象")
				.topRanking();
		});
	}

}
